package com.wq.httpclientcore.convert;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wq.httpclientcore.entities.po.ApiConfig;
import com.wq.httpclientcore.entities.po.AuthConfig;
import com.wq.httpclientcore.entities.po.DbConnectionConfig;
import com.wq.httpclientcore.entities.po.DbQueryConfig;
import com.wq.httpclientcore.entities.req.save.ApiConfigSaveREQ;
import com.wq.httpclientcore.entities.req.save.AuthConfigSaveREQ;
import com.wq.httpclientcore.entities.req.save.DbConnectionConfigSaveREQ;
import com.wq.httpclientcore.entities.req.save.DbQueryConfigSaveREQ;
import com.wq.httpclientcore.entities.vo.ApiConfigVO;
import com.wq.httpclientcore.entities.vo.AuthConfigVO;
import com.wq.httpclientcore.entities.vo.DbConnectionConfigVO;
import com.wq.httpclientcore.entities.vo.DbQueryConfigVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;
import org.springframework.beans.BeanUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface Convertor {
    Convertor INSTANCE = Mappers.getMapper(Convertor.class);
    ObjectMapper objectMapper = new ObjectMapper();

    @Mapping(target = "requestParams", expression = "java(convertStringToMap(saveREQ.getRequestParams()))")
    @Mapping(target = "requestHeaders", expression = "java(convertStringToMap(saveREQ.getRequestHeaders()))")
    @Mapping(target = "requestBody", expression = "java(convertStringToMap(saveREQ.getRequestBody()))")
    ApiConfig convert(ApiConfigSaveREQ saveREQ);

    @Mapping(target = "requestParams", expression = "java(convertMapToString(po.getRequestParams()))")
    @Mapping(target = "requestHeaders", expression = "java(convertMapToString(po.getRequestHeaders()))")
    @Mapping(target = "requestBody", expression = "java(convertMapToString(po.getRequestBody()))")
    @Mapping(target = "authConfig", source = "authConfig")
    ApiConfigVO convert(ApiConfig po);
    List<ApiConfigVO> convertApiConfigList(List<ApiConfig> list);

    DbConnectionConfig convert(DbConnectionConfigSaveREQ saveREQ);
    DbConnectionConfigVO convert(DbConnectionConfig po);
    List<DbConnectionConfigVO> convertDbConnectionConfigs(List<DbConnectionConfig> list);

    AuthConfig convert(AuthConfigSaveREQ saveREQ);
    AuthConfigVO convert(AuthConfig po);
    List<AuthConfigVO> convertAuthConfigs(List<AuthConfig> list);


    DbQueryConfig convert(DbQueryConfigSaveREQ saveREQ);
    DbQueryConfigVO convert(DbQueryConfig po);
    List<DbQueryConfigVO> convertDbQueryConfigs(List<DbQueryConfig> list);


    default IPage<ApiConfigVO> convertApiConfigPage(IPage<ApiConfig> page) {
        if (page == null || page.getRecords() == null || page.getRecords().isEmpty()) {
            return new Page<>();
        }
        List<ApiConfigVO> voList = convertApiConfigList(page.getRecords());
        Page<ApiConfigVO> voPage = new Page<>();
        BeanUtils.copyProperties(page, voPage);
        voPage.setRecords(voList);
        return voPage;
    }


    //  String 转换为 Map
    default Map<String, Object> convertStringToMap(String json) {
        try {
            if(StringUtils.isNotBlank(json)){
                return objectMapper.readValue(json, Map.class);
            }
            return new HashMap<>();
        } catch (JsonProcessingException e) {
            throw new RuntimeException("无法将字符串转换为 map", e);
        }
    }

    //Map 转换为 String
    default String convertMapToString(Map<String, Object> map) {
        try {
            if(map == null || map.isEmpty()){
                return "";
            }
            return objectMapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("无法将 map 转换为字符串", e);
        }
    }
}